package Tool;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.Map;
/**
 * Class description： 
 * ----项目间的通讯工具类
 *@author: liaoGaoZhi
 * 创建日期：2015-11-3 
 *
 */

public class httpUtils {
	
	
	public static void main(String[] args) {
		// 表示服务器端的url
		String PATH = "http://124.207.105.155/toas/examing/getPracticePaper.action?outlineId=2";
		String httpSend = httpSend(PATH);
		List<Map<String,Object>> jsonToMapList = FastJsonUtils.jsonToMapList(httpSend);
		String answer = "";
		String stem = "";
		String option = "";
		String answerjson = "";
		String optionListjson = "";
		for (Map<String, Object> map : jsonToMapList) {
            StringBuffer optionList = new StringBuffer();
//			System.out.println(map);
			//答案
			answerjson = map.get("answer").toString();
			Map<String, Object> answerMap = FastJsonUtils.jsonToMap(answerjson);
			answer = answerMap.get("objectiveKey").toString().substring(2,3);
			stem = map.get("stem").toString();
			optionListjson = map.get("optionList").toString();
			List<Map<String,Object>> optionMapList = FastJsonUtils.jsonToMapList(optionListjson);
			for (Map<String, Object> optionMap : optionMapList) {
                optionList.append(optionMap.get("key").toString()+". "+optionMap.get("content").toString()+";");
			}
			option=optionList.toString().substring(0,optionList.length()-1);
            try{
                BufferedWriter bw = new BufferedWriter (new OutputStreamWriter (new FileOutputStream ("answer.xlsx", true), "UTF-8"));
                String questions=answer;
                char[] question = questions.toCharArray();
                for(char c:question){
                    bw.append(c);
                }

//                char[] stemChar = stem.toCharArray();
//                char[] optionChar = option.toCharArray();
//                char[] answerChar = answer.toCharArray();
//                for(char c:stemChar){
//                    bw.append(c);
//                }
//                bw.append("/t");
//                for(char c:optionChar){
//                    bw.append(c);
//                }
//
//                bw.append("/t");
//                for(char c:answerChar){
//                    bw.append(c);
//                }
                bw.newLine();

                bw.flush();
                bw.close();
                bw.close();
            }catch (Exception e){
                System.out.println(e.toString());
            }
            System.out.println(stem);
            System.out.println(option);
            System.out.println(answer);
		}
	}
	
	/**
	 * Method description：
	 *  ---- 发送HPPT请求 
	 * @param bufUrl  URL
	 * @return
	 *	返回类型：String (json格式的字符串)
	 */
	public static String httpSend(String bufUrl) {
		StringBuffer tStringBuffer = new StringBuffer();
		InputStream in = null;
		try {
			URL url = new URL(bufUrl);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setRequestProperty("Content-Type", "text/xml;charset=utf-8");
			conn.setConnectTimeout(3000);
			conn.setReadTimeout(10000);
			if (HttpURLConnection.HTTP_OK == conn.getResponseCode()) {
				in = conn.getInputStream();
				if (in != null) {
					BufferedReader tBufferedReader = new BufferedReader(
							new InputStreamReader(in, "UTF-8"));
					String sTempOneLine = new String("");
					while ((sTempOneLine = tBufferedReader.readLine()) != null) {
						tStringBuffer.append(sTempOneLine);
					}
				}
			}
		} catch (MalformedURLException e) {
		} catch (IOException e) {
		}catch (Exception e) {
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
				}
			}
		}
		return tStringBuffer.toString();
	}
}
